class Solution1 {
    public int minCut(String s) {
        int n = s.length();
        boolean[][] isPal = new boolean[n][n];
        for(int i = n-1; i>=0; i--) {
            for(int j = i; j<n; j++) {
                if(s.charAt(i) == s.charAt(j)) {
                    if(i == j || i+1 == j) {
                        isPal[i][j] = true;
                    }else if(isPal[i+1][j-1]) {
                        isPal[i][j] = true;
                    }
                }
            }
        }

        int[] dp = new int[n];
        for(int i = 0; i<n; i++) {
            dp[i] = Integer.MAX_VALUE;
        }

        for(int i = 0 ;i<n; i++) {
            if(isPal[0][i]) {
                dp[i] = 0;
                continue;
            }
            for(int j = 0; j<i; j++) {
                if(isPal[j+1][i]) {
                    dp[i] = Math.min(dp[j]+1, dp[i]);
                }
            }
            //System.out.println(dp[i]);
        }

        return dp[n-1];
    }
}

class Solution {
    public int longestPalindromeSubseq(String s) {
        int n = s.length();
        int[][] dp = new int[n][n];

        for(int i = n-1; i>=0; i--) {
            for(int j = i; j<n; j++) {
                if(s.charAt(i) == s.charAt(j)) {
                    if(i == j) dp[i][j] = 1;
                    else if(i+1 == j) dp[i][j] = 2;
                    else dp[i][j] = dp[i-1][j-1] + 2;
                }else {
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }

        return dp[0][n-1];
    }
}


public class Test {
}
